package cn.shop.dys.activity;

import android.content.Intent;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.viewpagerindicator.TabPageIndicator;

import java.util.ArrayList;
import java.util.List;

import cn.shop.dys.fragment.AllIndentFragment;
import cn.shop.dys.fragment.BaseFragment;
import cn.shop.dys.fragment.WaitDeliverGoodsFragment;
import cn.shop.dys.fragment.WaitEvaluateFragment;
import cn.shop.dys.fragment.WaitPayFragment;
import cn.shop.dys.fragment.WaitReceiveGoodsFragment;
import cn.shop.lib_common.CommonUtil;
import cn.shop.dys.R;

/**
 *
 * 订单
 * Created by sunjian on 2016/4/20.
 *
 */
public class IndentActivity extends FragmentActivity implements ViewPager.OnPageChangeListener {
    @ViewInject(R.id.vp_indent_msg)
    private ViewPager mVpIndentMsg;
    @ViewInject(R.id.tpi_guide_title)
    private TabPageIndicator mTpiGuideTitle;
    @ViewInject(R.id.tv_common_index_titile)
    private TextView mTvTitle;
    @ViewInject(R.id.iv_common_settings)
    private ImageView mIvSettings;
    @ViewInject(R.id.iv_common_back)
    private ImageView mIvBack;
    private List<BaseFragment> mFragments=new ArrayList<>();
    private PopupWindow mPopupWindow;
    private String VP_TITLES[]=new String[]{"全部","待付款","待发货","待收货","待评价"};
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_indent);
        ViewUtils.inject(this);
        mTvTitle.setText("我的订单");
        init();
    }

    private void init() {
        initFragment();
        initEvent();
        initPage();
    }

    private void initPage() {
        Intent intent = getIntent();
        int indentIndex = intent.getIntExtra("indentIndex",-1);
        if(indentIndex>0){
            if(indentIndex<4){
                mVpIndentMsg.setCurrentItem(indentIndex+1,false);
            }
        }else {
            mVpIndentMsg.setCurrentItem(0,false);
        }
    }

    private void initFragment() {
        mFragments.add(new AllIndentFragment());
        mFragments.add(new WaitPayFragment());
        mFragments.add(new WaitDeliverGoodsFragment());
        mFragments.add(new WaitReceiveGoodsFragment());
        mFragments.add(new WaitEvaluateFragment());
    }

    private void initEvent() {
        mIvBack.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                /*if (mPosition != 0) {
                    mVpDetails.setCurrentItem(mPrePosition);
                } else {
                    finish();
                }*/
               // disposeBackEvent();
                finish();
            }
        });
        mIvSettings.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initPopupWindow(v);
            }
        });
        mVpIndentMsg.setAdapter(new FragmentPagerAdapter(getSupportFragmentManager()) {
            @Override
            public Fragment getItem(int position) {
                return mFragments.get(position);
            }

            @Override
            public int getCount() {
                return VP_TITLES.length;
            }

            @Override
            public CharSequence getPageTitle(int position) {
                return VP_TITLES[position];
            }

            @Override
            public void destroyItem(ViewGroup container, int position, Object object) {
               // super.destroyItem(container, position, object);
            }
        });
        mVpIndentMsg.setOffscreenPageLimit(4);
        mTpiGuideTitle.setViewPager(mVpIndentMsg);
        mTpiGuideTitle.setOnPageChangeListener(this);

    }
 //   List<Integer> mPositions=new ArrayList<>();
    public void disposeBackEvent() {
        /*int size = mPositions.size();
        if(size !=0){
            Integer integer = mPositions.get(size - 1);
            mVpIndentMsg.setCurrentItem(integer);
            mPositions.remove(integer);
        }else {
            finish();
        }*/
    }
    private void initPopupWindow (View view){
        View pwView = View.inflate(this, R.layout.view_popupwindow, null);
        View pwTop=pwView.findViewById(R.id.ll_pw_top);
        View pwBottom=pwView.findViewById(R.id.ll_pw_bottom);
        mPopupWindow = new PopupWindow(pwView, LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT, true);
        mPopupWindow.setBackgroundDrawable(new ColorDrawable());
        mPopupWindow.setAnimationStyle(R.style.PopupWindowAnimation);
        int[] location = new int[2];
        view.getLocationInWindow(location);
        mPopupWindow.showAsDropDown(view, (int) -CommonUtil.dpToPx(this, 100), (int) -CommonUtil.dpToPx(this, 0));
        pwTop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(IndentActivity.this, MessageActivity.class);
                startActivity(intent);
                mPopupWindow.dismiss();
            }
        });
        pwBottom.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(IndentActivity.this, MainActivity2.class);
                startActivity(intent);
                mPopupWindow.dismiss();
            }
        });
        //  popupWindow.showAtLocation(view, Gravity.RIGHT| Gravity.TOP, location[0]+40, location[1] + 40);
    }
    /*private int mPrePosition;
    private int mPosition;*/

    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
    //    Log.d("test","position"+position);
        /*this.mPrePosition=this.mPosition;
        this.mPosition=position;
        if(this.mPrePosition<this.mPosition){
            mPositions.add(this.mPrePosition);
            //      Log.d("test", "mIndex" + mIndex);
            if(mIndex>0&&this.mPrePosition==0){
                mPositions.remove(this.mPrePosition);
            }
        }*/
    }

    @Override
    public void onPageScrollStateChanged(int state) {

    }
}
